package org.jeecg.modules.pipe.service.impl;

import org.jeecg.modules.pipe.entity.CraftInfo;
import org.jeecg.modules.pipe.entity.CraftDevice;
import org.jeecg.modules.pipe.mapper.CraftDeviceMapper;
import org.jeecg.modules.pipe.mapper.CraftInfoMapper;
import org.jeecg.modules.pipe.service.ICraftInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 工艺图
 * @Author: jeecg-boot
 * @Date:   2024-06-04
 * @Version: V1.0
 */
@Service
public class CraftInfoServiceImpl extends ServiceImpl<CraftInfoMapper, CraftInfo> implements ICraftInfoService {

	@Autowired
	private CraftInfoMapper craftInfoMapper;
	@Autowired
	private CraftDeviceMapper craftDeviceMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CraftInfo craftInfo, List<CraftDevice> craftDeviceList) {
		craftInfoMapper.insert(craftInfo);
		if(craftDeviceList!=null && craftDeviceList.size()>0) {
			for(CraftDevice entity:craftDeviceList) {
				//外键设置
				entity.setCraftId(craftInfo.getId());
				entity.setCraftShow("1".equals(entity.getCraftShow())? 1 : 0);
				craftDeviceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CraftInfo craftInfo,List<CraftDevice> craftDeviceList) {
		craftInfoMapper.updateById(craftInfo);
		
		//1.先删除子表数据
		craftDeviceMapper.deleteByMainId(craftInfo.getId());
		
		//2.子表数据重新插入
		if(craftDeviceList!=null && craftDeviceList.size()>0) {
			for(CraftDevice entity:craftDeviceList) {
				//外键设置
				entity.setCraftId(craftInfo.getId());
				craftDeviceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		craftDeviceMapper.deleteByMainId(id);
		craftInfoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			craftDeviceMapper.deleteByMainId(id.toString());
			craftInfoMapper.deleteById(id);
		}
	}
	
}
